﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Security.Cryptography;

namespace ConsumeSys.Common
{
    /// <summary>
    /// 公共操作类
    /// </summary>
    public class Globals
    {
        /// <summary>
        /// 登录用户的cookie-key
        /// </summary>
        public static string loginUserCookieKey = "LoginMsg";

        /// <summary>
        /// 创建MD5对象
        /// </summary>
        private static MD5 md5 = MD5.Create();

        /// <summary>
        /// 保存登录用户
        /// </summary>
        /// <param name="val">需要保存的值</param>
        public static void SetLoginUserCookie(string val)
        {
            SetCookie(loginUserCookieKey, DESEncrypt.Encrypt(val), DateTime.Now.AddDays(1));
        }

        /// <summary>
        /// 退出登录用户
        /// </summary>
        public static void ExitLoginUser() 
        {
            ClearCookie(loginUserCookieKey);
        }

        /// <summary>
        /// 返回保存登录用户的cookie
        /// </summary>
        public static string GetLoginUserCookie()
        {
            return DESEncrypt.Decrypt(GetCookieValue(loginUserCookieKey));
        }

        /// <summary>
        /// 用户是否登录w
        /// </summary>
        /// <returns>bool</returns>
        public static bool IsLoginUser()
        {
            string key = GetLoginUserCookie();
            if (!string.IsNullOrWhiteSpace(key))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取指定Cookie值
        /// </summary>
        /// <param name="cookiename">cookie名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookieValue(string cookiename)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookiename];
            string cookievalue = string.Empty;
            if (cookie != null)
            {
                cookievalue = cookie.Value;
            }
            return cookievalue;
        }

        /// <summary>
        /// 添加一个Cookie
        /// </summary>
        /// <param name="cookiename">名称</param>
        /// <param name="cookievalue">值</param>
        /// <param name="expires">过期时间</param>
        public static void SetCookie(string cookiename, string cookievalue, DateTime expires)
        {
            HttpCookie cookie = new HttpCookie(cookiename)
            {
                Value = cookievalue,
                Expires = expires
            };
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 清除cookie
        /// </summary>
        /// <param name="cookieName">需要清除的cookie名称</param>
        public static void ClearCookie(string cookieName)
        {
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
            {
                HttpCookie mycookie;
                mycookie = HttpContext.Current.Request.Cookies[cookieName];
                TimeSpan ts = new TimeSpan(0, 0, 0, 0);//时间跨度 
                mycookie.Expires = DateTime.Now.Add(ts);//立即过期 
                HttpContext.Current.Response.Cookies.Remove(cookieName);//清除 
                HttpContext.Current.Response.Cookies.Add(mycookie);//写入立即过期的*/
                HttpContext.Current.Response.Cookies[cookieName].Expires = DateTime.Now.AddDays(-1);
            }
        }

        /// <summary> 
        /// 使用utf8编码将字符串散列 
        /// </summary> 
        /// <param name="sourceString">要散列的字符串</param> 
        /// <returns>散列后的字符串</returns> 
        public static string HashString(string sourceString)
        {
            return HashString(Encoding.UTF8, sourceString);
        }

        /// <summary> 
        /// 使用指定的编码将字符串散列 
        /// </summary> 
        /// <param name="encode">编码</param> 
        /// <param name="sourceString">要散列的字符串</param> 
        /// <returns>散列后的字符串</returns> 
        public static string HashString(Encoding encode, string sourceString)
        {
            byte[] source = md5.ComputeHash(encode.GetBytes(sourceString));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < source.Length; i++)
            {
                sBuilder.Append(source[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
    }
}
